//
//  LandMarkDetail.swift
//  Landmarks
//
//  Created by XN on 2019/6/5.
//  Copyright © 2019 pc. All rights reserved.
//

import SwiftUI


struct LandMarkDetail : View {
    
    @Environment(ModelData.self) var modelData
    var landmark : Landmark
    var landmarkIndex:Int? {
        get{
            modelData.landmarkData.firstIndex { $0.id == landmark.id }
        }
    }
    
    var body: some View {
        @Bindable var modelData = modelData
        
        ScrollView {
            MapView(coordinate: landmark.locationCoordinate).frame(height: 300)
            
            CircleImage(image: landmark.image(forSize: 250))
                .offset(y: -130)
                .padding(.bottom, -130)
            
            LazyVStack(alignment: .leading) {
                LazyHStack{
                    Text(landmark.name)
                        .font(.title)
                    FavoriteButton(isSet: landmarkIndex != nil ? $modelData.landmarkData[landmarkIndex!].isFavorite : .constant(false))
                }
                
                LazyHStack {
                    Text(landmark.park)
                    Spacer()
                    Text(landmark.state)
                }
                .font(.subheadline)
                .foregroundStyle(.secondary)
                
                Divider()
                
                Text("About \(landmark.name)")
                    .font(.title2)
                Text(landmark.description ?? "")
            }
            .padding()
            Spacer()
        }
        .navigationBarTitle(landmark.name, displayMode: .inline)
        .navigationBarItems(trailing: FavoriteButton(isSet: landmarkIndex != nil ? $modelData.landmarkData[landmarkIndex!].isFavorite : .constant(false)))
    }
}

#Preview {
    let modelData = ModelData()
    return LandMarkDetail(landmark: modelData.landmarkData[0])
        .environment(modelData)
}
